<?php

namespace libs;
    
    class StringMethods {
    
        private static $_delimiter = "#";
        
        private static $_plural_patterns = array(
            'Aktualnosc' => '$0i',
            'User' => '$0s',
            'Strona' => 'strony'
        );
        
        public function __construct() {
        }
        
        private function __clone() {
            
        }
        
        
        private static function _normalize($pattern) {
            return self::$_delimiter.trim($pattern,self::$_delimiter).self::$_delimiter;
        }
        
        private static function getDelimiter() {
            return self::$_delimiter;
        }
        
        private static function setDelimiter($delimiter) {
            self::$_delimiter = $delimiter;
        }
        
        public static function match($string,$pattern) {
            
            preg_match_all(self::_normalize($pattern),$string, $matches);
           
            if(!empty($matches[1])) {
                return $matches[1];
            }
            if(!empty($matches[0])) {
                return $matches[0];
            }
            
            return null;
        }
        
        public static function split($string,$pattern,$limit = null) {
            $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
            return preg_split(self::_normalize($pattern),$string,$limit,$flags);
        }
    
        public static function singular()
        {
        }
        
        public static function plural($class)
        {
            foreach(self::$_plural_patterns as $singular => $plural)
            {
                if(preg_match('/'.$singular.'/',$class,$matches))
                {
                    $match = $matches[0];
                    if(count($matches) > 1) 
                    {
                        $match = $matches[1];
                    }
                    $tabela = preg_replace('/'.$singular.'/',$plural,$match);
                    return strtolower($tabela);
                }
            }
            
            return $class;
        }
    }
    